﻿using HelloMinasan.Web.Song.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HelloMinasan.Business;
using HelloMinasan.Web.Song.ViewModels;
using HelloMinasan.Domain;
using HelloMinasan.Framework.Web;
using HelloMinasan.Framework.Web.WebHelpers;
using HelloMinasan.Resources;
using HelloMinasan.Business.Extend;

namespace HelloMinasan.Web.Song.Controllers
{
    public class CommentController : ApplicationController
    {
        private IUserService userService;
        private ISongService songService;

        public CommentController(ISongService songService, IUserService userService)
        {
            this.songService = songService;
            this.userService = userService;
        }

        //public CommentController()
        //    : this(new UserIRepository(), new SongReportRepository(), new SongCommentRepository())
        //{
        //}

        //private CommentController(
        //    IRepository._UserIRepository userIRepository, 
        //    IRepository._SongReportRepository songReportRepository,
        //    IRepository._SongCommentIRepository songCommentRepository)
        //{
        //    this._userRepository = userIRepository;
        //    this._songReportRepository = songReportRepository;
        //    this._songCommentRepository = songCommentRepository;
        //}

        // GET: /Comment/
        public ActionResult CommentListPartial(int songId, int currentPage)
        {
            WrapCommentModels model = new WrapCommentModels()
            {
                SongId = songId,
                PagingControl = new PagingHelper() { CurrentPageNumber = currentPage }
            };
            return PartialView(model);
        }

        [HttpGet]
        public ActionResult CreateCommentPartial(int songId)
        {
            return PartialView(new CommentModels() { SongId = songId });
        }

        [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult CreateCommentPartial(int songId, string newComment)
        {
            int commentId = 0;
            //var user = _userRepository.FindUserById(User.Identity.Name);
            var user = userService.Get(CurrentUser.Id);
            if (user != null)
            {
                //TO DO here
                SongComment model = new SongComment()
                {
                    User = user,
                    CommentContent = newComment,
                    PostedDate = DateTime.Now,
                    SongId = songId
                };
                commentId = songService.AddComment(model);
            }
            WrapCommentModels returnModel = new WrapCommentModels()
            {
                SongId = songId,
                PagingControl = new PagingHelper()
            };
            returnModel.JustPerformBadWordItem = CheckingBadWords(newComment, commentId, songId);

            return PartialView("CommentListPartial", returnModel);
        }

        private bool CheckingBadWords(string newComment, int commentId, int songId)
        {
            //foreach (var itemBadWords in MvcApplication.FilterWords)
            foreach (var itemBadWords in songService.GetBadWord())
            {
                if (newComment.Contains(itemBadWords.Words + " ") ||
                    newComment.Contains(" " + itemBadWords.Words + " "))
                {
                    //_songReportRepository.AddReport(new SongReport()
                    songService.AddReport(new SongReport()
                    {
                        CommentID = commentId,
                        ReportDate = DateTime.Now,
                        ReportType = "",
                        SongID = songId
                    });
                    return true;
                }
            }
            return false;
        }


        public ActionResult UpdateComment(int commentId, string newComment)
        {
            ResultObject result = new ResultObject();
            result.Result = false;
            if (Request.IsAuthenticated)
            {
                try
                {
                    //var orginalComment = _songCommentRepository.GetCommentContent(commentId);
                    var orginalComment = songService.GetCommentContent(commentId);
                    if (CurrentUser.Id != orginalComment.User.Id)
                    {
                        result.Message = Resource.Song_OwnerRequest;//Resource.Song_OwnerRequest;
                    }
                    else
                    {
                        songService.UpdateComment(commentId, newComment);
                        if (!CheckingBadWords(newComment, commentId, orginalComment.Song.Id))
                        {
                            result.Result = true;
                            result.Message = Resource.Song_SuccessfullyEvent;
                        }
                        else
                        {
                            result.Message = Resource.Song_BadWordsDetected;
                        }
                    }
                }
                catch
                {
                    result.Message = Resource.Song_UpdateError;
                }
            }
            else
            {
                result.Message = Resource.Song_RequiredAuthentication;
            }

            return Json(result);
        }

        [Authorize]
        public ActionResult ReportCommentFromCommunity(int commentId)
        {
            ResultObject result = new ResultObject();
            result.Result = false;

            if (Request.IsAuthenticated)
            {
                var comment = songService.GetCommentContent(commentId);
                if (comment != null)
                {
                    result.Result = true;
                    songService.AddReport(new SongReport()
                    {
                        CommentID = commentId,
                        ReportDate = DateTime.Now,
                        ReportType = "3",
                        SongID = comment.Song.Id,
                        //UserReport = CurrentUser.Id,
                        User = userService.Get(CurrentUser.Id),
                        ReportStatus = true
                    });
                    result.Message = Resource.Song_SuccessfullyEvent;
                }
                else
                {
                    result.Message = "Data is not existed";
                }
            }
            else
            {
                result.Message = Resource.Song_RequiredAuthentication;
            }

            return Json(result);
        }
    }
}
